#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
ll m;
string s;
ll pre1, pre2;

int main()
{
    IOS;
    ll ans = 0;
    cin >> m;
    cin >> s;
    ll n = s.length();
    for (ll i = 0; i < n; i++)
    {
        ll x = s[i] - '0';
        if (m >= x)
        {
            x == 1 ? pre1++ : pre2++;
            m -= x;
            ans += (pre1 + pre2);
        }
        else
        {
            if (pre2 > 0)
            {
                pre2--;
                x == 1 ? pre1++ : pre2++;
                m = m + 2 - x;
                ans += (pre1 + pre2);
            }
            else if (pre1 > 0)
            {
                ans += (pre1 + pre2);
            }
            else
                continue;
        }
    }
    cout << ans << endl;
    getchar();
    getchar();
    return 0;
}
